package com.insigma.business.common.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author: huangjh
 * @description:
 * @date: 2024-04-12 10:08
 */
public class ClassUtils {
    public static String RootPath = new File("").getAbsolutePath();
    public static String classPath = Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("")).getPath();

    private static void getAllClasses(File file, List<Class<?>> classes) throws ClassNotFoundException {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    getAllClasses(f, classes);
                }
            }
        } else {
            if (file.getName().endsWith(".class")) {
                String className = file.getPath().replace(".class","").replace(classPath.replace("/","\\").substring(1),"").replace("\\",".");
                classes.add(Class.forName(className));
            }
        }
    }

    public static List<Class<?>> getAllClasses() throws ClassNotFoundException {
        List<Class<?>> list = new ArrayList<>();
        getAllClasses(new File(classPath), list);
        return list;
    }

    public static List<Class<?>> getAllClasses(String path) throws ClassNotFoundException {
        List<Class<?>> list = new ArrayList<>();
        getAllClasses(new File(path), list);
        return list;
    }
}

